package com.smg.community.controller.thread;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javacommon.util.GroupUtil;
import javacommon.util.HtmlToText;

import org.mortbay.log.Log;
import org.springframework.beans.factory.annotation.Autowired;

import com.smg.community.model.SmgKeyword;
import com.smg.community.model.SmgMember;
import com.smg.community.model.SmgMemberBehavior;
import com.smg.community.model.SmgTopic;
import com.smg.community.model.SmgTopicActivityMember;
import com.smg.community.model.SmgTopicActivitySetting;
import com.smg.community.model.SmgTopicReply;
import com.smg.community.service.SmgGroupCategoryManager;
import com.smg.community.service.SmgMemberBehaviorManager;
import com.smg.community.service.SmgMemberManager;
import com.smg.community.service.SmgTopicActivityMemberManager;
import com.smg.community.service.SmgTopicManager;
import com.smg.community.service.SmgTopicReplyManager;
import com.smg.community.setting.Constant;
import com.smg.community.setting.PointSetting;
import com.smg.community.util.ProjectUtil;
import com.smg.community.util.StringUtil;

public class AutoJoinActivityThread  extends Thread
{
	
	public SmgTopicManager smgTopicManager;
	public SmgTopicActivityMemberManager smgTopicActivityMemberManager;
	public int  AUTO_REPLY_CNT;
	public int  topicId;
	public SmgMemberManager smgMemberManager;
	private SmgMember loginMember;
	private SmgMemberBehaviorManager smgMemberBehaviorManager;
	
	private static String[] autoReplyContent = {
		"在遇到你之前，我对人世间是否有真正的圣人是怀疑的；而现在，我终于相信了！我曾经忘情于汉廷的歌赋，我曾经惊讶于李杜的诗才，我曾经流连于宋元的词曲；但现在，我才知道我有多么浅薄！ ",
		"大师的话真如“大音希声扫阴翳”，犹如”拨开云雾见青天”，使我等网民看到了希望，看到了未来！晴天霹雳，醍醐灌顶或许不足以形容大师文章的万一；巫山行云，长江流水更难以比拟大师的文才！黄钟大吕，振聋发聩！你烛照天下，明见万里；雨露苍生，泽被万方！透过你深邃的文字，我仿佛看到了你鹰视狼顾，龙行虎步的伟岸英姿；仿佛看到了你手执如椽大笔，写天下文章的智慧神态；仿佛看见了你按剑四顾，江山无数的英武气概！ ",
		"楼主万岁~撒花~",
		"哦呵呵呵呵``````````` ",
		"顶LZ~",
		"谢谢楼主分享。",
		"我hold住~~",
		"我就看看，不说话~",
		"看的有点晕啊~~",
		"表示费解。。。。@_@",
		"坐等楼下强贴出现",
		"求围观",
		"赞哟赞哟~~",
		"前排留名",
		"隐身党飘过。。。",
		"这个可以有~~",
		"Mark~",
		"马克之…"
		
	};
	
	public AutoJoinActivityThread (SmgMemberBehaviorManager smgMemberBehaviorManager,SmgTopicManager smgTopicManager,SmgTopicActivityMemberManager smgTopicActivityMemberManager,SmgMemberManager smgMemberManager,int AUTO_REPLY_CNT,int topicId,SmgMember loginMember){
		this.smgTopicManager = smgTopicManager;
		this.smgTopicActivityMemberManager = smgTopicActivityMemberManager;
		this.AUTO_REPLY_CNT = AUTO_REPLY_CNT;
		this.topicId = topicId;
		this.loginMember = loginMember;
		this.smgMemberManager = smgMemberManager;
		this.smgMemberBehaviorManager = smgMemberBehaviorManager;
	}
	@Override
	public void run() {
		int i = 0;
		System.out.println("=======启动刷楼线程=======");
		Random random = new Random();
		int sleepTime = random.nextInt(10)+30;
		try {
			while(true){
				sleep(sleepTime*1000);
				saveToTopicReply();
				i++;
				if(i>AUTO_REPLY_CNT)
					break;
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		
	}
	private void saveToTopicReply(){
		try {
			SmgMember loginMember = smgMemberManager.getRandomMember();
			SmgTopic topic = smgTopicManager.getById(topicId);
			SmgTopicReply reply = new SmgTopicReply();
			Random random = new Random();
			int randomId = random.nextInt(autoReplyContent.length);
			SmgTopicActivityMember activityMember = new  SmgTopicActivityMember();
			activityMember.setFkTopicId(topicId);
			activityMember.setFkMemberId(loginMember.getMemberId());
			activityMember.setJoinDate(new Date());
			activityMember.setHeadImage(loginMember.getHeadImage());
			activityMember.setNickName(loginMember.getNickName());
			smgTopicActivityMemberManager.save(activityMember);
			SmgTopic smgTopic = smgTopicManager.getById(topicId); 
			SmgMemberBehavior smgMemberBehavior = new SmgMemberBehavior();
			smgMemberBehavior.setCont("参加了活动  "+smgTopic.getTopicTitleAfterFilter());
			smgMemberBehavior.setHeadImage(loginMember.getHeadImage());
			smgMemberBehavior.setMemberId(loginMember.getMemberId());
			smgMemberBehavior.setNickName(loginMember.getNickName());
			smgMemberBehavior.setContPreview(HtmlToText.getPlainText(smgTopic.getTopicContAfterFilter()));
			smgMemberBehavior.setCreateMemberId(loginMember.getMemberId());
			smgMemberBehavior.setCreateMemberName(loginMember.getNickName());
			smgMemberBehavior.setFkGroupId(smgTopic.getFkGroupId());
			smgMemberBehavior.setGroupName(smgTopic.getGroupName());
			smgMemberBehavior.setSourcefrom("pc");
			smgMemberBehavior.setType(Constant.BEHAVIOR_JOIN_ACTIVITY);
			smgMemberBehavior.setUrl("http://www.tac1057.com/topic/detail/"+topicId);
			smgMemberBehavior.setImage(ProjectUtil.getImageFromTopicCont(smgTopic.getTopicContAfterFilter()));
			smgMemberBehavior.setRelatedId(smgTopic.getTopicId());
			smgMemberBehavior.setTitle(smgTopic.getTopicTitleAfterFilter());
			smgMemberBehaviorManager.save(smgMemberBehavior);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
